#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;

vector<int> sortByBits(vector<int>& arr) {
    vector<int> bit(10001, 0);
    for (int x : arr) {
        bit[x] = __builtin_popcount(x);
    }
    sort(arr.begin(), arr.end(), [&](int x, int y) {
        if (bit[x] != bit[y]) {
            return bit[x] < bit[y];
        } else {
            return x < y;
        }
    });
    return arr;
}

int main() {
    int n;
    cin >> n;
    vector<int> arr(n);
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    vector<int> vec(n);
    vec = sortByBits(arr);
    for (auto x : vec) {
        cout << x << " ";
    }
    return 0;
}
